package com.ytzl.controller;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ytzl.common.CodeResultEnums;
import com.ytzl.common.R;
import com.ytzl.common.ResponseDataUtils;
import com.ytzl.dto.Dto;
import com.ytzl.modul.ItripUser;
import com.ytzl.service.ItripUserService;
import com.ytzl.vo.ItripUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Api(tags = "UserController")
@RestController
@RequestMapping("/api")
@Slf4j
public class UserController {
    @Resource
    private ItripUserService service;

    private Dto dto;

    @ApiOperation("查询所有用户信息")
    @GetMapping("/getAll")
    public R<ItripUser> getUserList(){
        System.out.println("获取全部=========》");
        return ResponseDataUtils.buildSuccess(service.list());
    }

    @GetMapping("/getById")
    public ItripUser getUserById(Integer id){
        System.out.println("通过id获取==========》");
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",id);
        ItripUser user=(ItripUser)service.list(queryWrapper).get(0);
        return user;
    }

    /*登录*/
    @ApiOperation("登录用户")
    @PostMapping("/dologin")
    public R Login(String userCode,String userPassword){
        System.out.println("通过id获取==========》");
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("userCode",userCode);
        ItripUser user=service.getOne(wrapper);
        System.out.println("返回值："+user);
        if (null!=userCode && null!=userPassword){
            if (user!=null){
                if (user.getUsercode().equalsIgnoreCase(userCode)){
                    String md5= SecureUtil.md5(userPassword);
                    if (user.getUserpassword().equalsIgnoreCase(md5)){
                        user.setUserpassword(null);
                        log.info("账号："+user.getUsercode()+",用户名"+user.getUsername()+",密码"+user.getUserpassword());
                        return ResponseDataUtils.buildSuccess(user);
                    }return ResponseDataUtils.buildError(CodeResultEnums.INPUT_PARAMS_IS_EMPTY);
                }return ResponseDataUtils.buildError(CodeResultEnums.INPUT_PARAMS_IS_EMPTY);
            }return ResponseDataUtils.buildError(CodeResultEnums.INPUT_PARAMS_IS_EMPTY);
        }return ResponseDataUtils.buildError(CodeResultEnums.INPUT_PARAMS_IS_EMPTY);
    }

    /*注销*/
    @GetMapping("/logout")
    public Object Logout(){
        return null;
    }

    /*注册*/
    @ApiOperation("注册信息")
    @PostMapping("/registerByName")
    public R Register(@RequestBody ItripUserVo userVo){
        //1.检测用户是否注册
        QueryWrapper byUserCode=new QueryWrapper();
        byUserCode.eq("userCode",userVo.getUsercode());
        ItripUser user=service.getOne(byUserCode);
        if (user!=null){
            //log.info("{},{}","111","222");
            log.info("该用户已注册，请输入一个新的账号名称。");
            return ResponseDataUtils.buildError(CodeResultEnums.USER_CODE_EXIST);
        }
        //2.使用表单用户的信息组合数据
        ItripUser userModel=new ItripUser();
        userModel.setUsercode(userVo.getUsercode());
        userModel.setUsername(userVo.getUsername());

        /*MD5加密*/
        String md5= SecureUtil.md5(userVo.getUserpassword());
        log.info("{}",md5);
        userModel.setUserpassword(md5);

        userModel.setUsertype(1);
        userModel.setActivated(1);
        userModel.setCreationdate(new Date());
        userModel.setModifydate(new Date());
        return ResponseDataUtils.buildSuccess(service.save(userModel));
    }
}
